package editor.dxf;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.kabeja.Tests;
import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.DXFLayer;
import org.kabeja.dxf.DXFLine;
import org.kabeja.parser.DXFParser;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;

import editor.graphics.Dibujable;

public class DxfModelParser implements Dibujable {

	//1px 
	private static int scale = 10;
	
	private ArrayList<Integer> ptos = new ArrayList<Integer>();
	private int offsetX, offsetY;
	private double rotation = -Math.PI/2;
	BufferedImage img = null;
//	BufferedImage off_Image;
	
	public DxfModelParser(String file, int MapScale, int grados){
		offsetX = offsetY = 0;
		this.rotation = Math.PI* grados/180.0f;
		File f = new File(".");
		System.out.println(f.getAbsolutePath());
		read(file, (float)MapScale/(scale)); //"./CuerpoA_piso3.dxf"
/*
		try {
		    img = ImageIO.read(new File("imagen.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		img = new BufferedImage(500, 500,
			      BufferedImage.TYPE_INT_BGR);

		Graphics2D g2 = img.createGraphics();
		dibujar2(g2);
	}
	
	public void setOffsetX(int offsetX){
		this.offsetX = offsetX;
	}
	
	public void setOffsetY(int offsetY){
		this.offsetY = offsetY;
	}
	public int getOffsetX(){
		return offsetX;
	}
	
	public int getOffsetY(){
		return offsetY;
	}
	
	public void move(int offX, int offY){
		this.offsetX+= offX;
		this.offsetY+= offY;
	}
	
	
	
	
	public void read(String in, float scaleby) {
	Parser parser = ParserBuilder.createDefaultParser();
		// parse
		try {
			parser.parse(in, DXFParser.DEFAULT_ENCODING);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// get the documnet and the layer
		DXFDocument doc = parser.getDocument();
		//doc.getDXFLayerIterator()
	//	DXFLayer layer = doc.getDXFLayer(layerid);

		java.util.Iterator<DXFLayer> it = doc.getDXFLayerIterator();
		while(it.hasNext()){
			List<DXFLine> plines = it.next().getDXFEntities(DXFConstants.ENTITY_TYPE_LINE);
			if(plines != null)for(DXFLine line: plines){
				ptos.add( (int) (line.getStartPoint().getX()*scaleby));
				ptos.add( -(int) (line.getStartPoint().getY()*scaleby));
				ptos.add( (int) (line.getEndPoint().getX()*scaleby));
				ptos.add( -(int) (line.getEndPoint().getY()*scaleby));
			}
		}
		
		
	}

	public void dibujar2(Graphics2D g) {
		

		g.setColor(Color.WHITE);
		for(int i = 0; i< ptos.size()/4; i++){
			g.drawLine(ptos.get(i*4), ptos.get(i*4+1), 
					ptos.get(i*4+2), ptos.get(i*4+3));
		}
		
	}

	@Override
	public void dibujarSelccionado(Graphics2D g) {
		dibujar2(g);

	}

	@Override
	public void dibujar(Graphics2D g) {	
		g.translate(offsetX, offsetY);
		g.rotate(rotation);
		g.drawImage(img, 0,-500, img.getWidth(), img.getHeight(), null);
		g.rotate(-rotation);
		g.translate(-offsetX, -offsetY); //revierto la transf
		
	}

}
